export type EventCallback = (...args: any[]) => void;
export default class SimpleEvent {
    private constructor() {
    }

    private static readonly events: { [eventName: string]: EventCallback[]; } = {};

    public static addEventListener(eventName: string, callback: EventCallback) {
        if (!this.events.hasOwnProperty(eventName)) {
            this.events[eventName] = [];
        }

        const exists = this.events[eventName];
        if (-1 === exists.indexOf(callback)) {
            exists.push(callback);
        }
    }

    public static removeEventListener(eventName: string, callback: EventCallback) {
        if (this.events.hasOwnProperty(eventName)) {
            const callbacks = this.events[eventName];
            if (-1 < callbacks.indexOf(callback)) {
                callbacks.splice(callbacks.indexOf(callback), 1);
            }

            if (0 === callbacks.length) {
                delete this.events[eventName];
            }
        }
    }

    public static trigger(eventName: string, ...args: any[]) {
        if (this.events.hasOwnProperty(eventName)) {
            this.events[eventName].slice().forEach(function (cb) {
                cb(...args);
            });
        }
    }
}